package com.regis.controller;

import cn.hutool.core.lang.Dict;
import cn.hutool.extra.template.Template;
import cn.hutool.extra.template.TemplateConfig;
import cn.hutool.extra.template.TemplateEngine;
import cn.hutool.extra.template.TemplateUtil;
import com.regis.comment.CodeMsg;
import com.regis.comment.GlobalException;
import com.regis.comment.uilts.VerificationCodeUtil;
import com.regis.entity.db.User;
import com.regis.entity.ro.EmailRo;
import com.regis.entity.vo.ResultVo;
import com.regis.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.CacheManager;
import org.springframework.http.HttpRequest;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Map;

/**
 * @author 计算机系 ITAEM 纱雾休比我老婆
 * @Description 用户接口
 * @date 2022/8/4 1:53
 */
@RestController
@RequestMapping("/user")
@Slf4j
public class UserController {

    @Autowired
    private UserService userService;
    @Autowired
    private CacheManager cacheManager;

    /**
     * 发送手机短信验证码
     * @param user
     * @return
     */
//    @PostMapping("/sendMsg")
//    public ResultVo<String> sendMsg(@RequestBody User user, HttpSession session){
//        //获取手机号
//        String phone = user.getPhone();
//
//        if (StringUtils.isNotEmpty(phone)){
//            //生成验证码
//            String code = VerificationCodeUtil.getVerificationCode(4).toString();
//
//            //调用阿里云api发送短信
////            SMSUtil.sendMessage("签名(未申请)","",phone,code);
//            log.info("发送的验证码为：{}",code);
//
//            //保存验证码
//            session.setAttribute(phone,code);
//
//            return ResultVo.success("手机验证码发送成功");
//        }
//
//        return ResultVo.error("短信发送失败");
//    }

    /**
     * 发送邮箱验证码
     * @param user User
     * @param session HttpSession
     * @return code
     */
    @PostMapping("/sendMsg")
    public ResultVo<String> sendMsg(@RequestBody User user, HttpSession session){
        String email = user.getPhone();
        if (email == null) {
            throw new GlobalException(new CodeMsg("邮箱为空"));
        }
        log.info(email);

        // 获取发送邮箱验证码的HTML模板
        TemplateEngine engine = TemplateUtil.createEngine(new TemplateConfig("template",
                TemplateConfig.ResourceMode.CLASSPATH));
        Template template = engine.getTemplate("email-code.ftl");

        //获取验证码
        Integer code = VerificationCodeUtil.getVerificationCode(6);
        log.info("收到的验证码为：{}",code);

        //发送验证码
        userService.send(new EmailRo(email, "邮箱验证码登录", template.render(Dict.create().set("code", code)), code));

        return ResultVo.success("验证码发送成功[{}]".replaceAll("\\{}",code.toString()));
    }

    /**
     * 移动端用户登录
     * @param map User
     * @param session HttpSession
     * @return String
     */
    @PostMapping("/login")
    public ResultVo<User> login(@RequestBody Map<String,String> map, HttpSession session){
        //获取邮箱
        String email = map.get("phone");
        //获取验证码
        String code = map.get("code");
        ResultVo<User> user = userService.login(email, code);
        if (user.getCode() == 200){
            session.setAttribute("user",user.getData().getId());
        }
        return user;
    }

    /**
     * 移动端用户退出登录
     * @return ResultVo<String>
     */
    @PostMapping("/loginout")
    private ResultVo<String> loginOut(HttpServletRequest request){
        request.getSession().removeAttribute("user");
        return ResultVo.success("退出成功");
    }
}
